<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:cc="http://xmlns.jcp.org/jsf/composite"
      xmlns:p="http://primefaces.org/ui">

    <!-- INTERFACE -->
    <cc:interface>
        <cc:attribute name="var" required="true"/>
        <cc:attribute name="current" required="true"/>
        <cc:attribute name="values" required="true"/>
        <cc:attribute name="emptyGridMessage" required="true"/>
        <cc:attribute name="newPopupMenuText" required="true"/>
        <cc:attribute name="copyPopupMenuText" required="true"/>
        <cc:attribute name="deletePopupMenuText" required="true"/>
        <cc:attribute name="saveButtonText" required="true"/>
        <cc:attribute name="gridHeaderText"/>
        <cc:attribute name="gridFooterText"/>
        <cc:attribute name="formHeaderText"/>
        <cc:facet name="formFields" required="true"/>
        <cc:attribute name="prepareCreate" required="true" method-signature="void action()"/>
        <cc:attribute name="prepareCopy" required="true" method-signature="void action()"/>
        <cc:attribute name="deleteSelected" required="true" method-signature="void action()"/>
        <cc:attribute name="save" required="true" method-signature="void action()"/>
    </cc:interface>

    <!-- IMPLEMENTATION -->
    <cc:implementation>
        <h:form id ="gridForm">
            <h:panelGroup id="gridPanel"> 
                <p:dataTable id="grid" value="#{cc.attrs.values}"
                             emptyMessage="#{cc.attrs.emptyGridMessage}"
                             selection="#{cc.attrs.current}" selectionMode="single" 
                             paginator="true" rows="10" rowsPerPageTemplate="5,10,15" paginatorPosition="top"
                             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                             resizableColumns="true" lazy="true">

                    <c:set target="#{component}" property="var" value="#{cc.attrs.var}"/>

                    <p:ajax event="rowSelect" update=":#{cc.clientId}:gridForm:formPanel"/> 
                    <p:ajax event="rowUnselect" update=":#{cc.clientId}:gridForm:formPanel"/> 

                    <f:facet name="header" rendered="#{not empty cc.attrs.gridHeaderText}">
                        #{cc.attrs.gridHeaderText}
                    </f:facet>
                    <cc:insertChildren/>
                    <f:facet name="footer" rendered="#{not empty cc.attrs.gridFooterText}">
                        #{cc.attrs.gridFooterText}
                    </f:facet>
                </p:dataTable>
                <p:contextMenu for="grid">  
                    <p:menuitem value="#{cc.attrs.newPopupMenuText}" update=":#{cc.clientId}:gridForm:formPanel :#{cc.clientId}:gridForm:gridPanel" actionListener="#{cc.attrs.prepareCreate}"/>  
                    <p:menuitem value="#{cc.attrs.copyPopupMenuText}" update=":#{cc.clientId}:gridForm:formPanel :#{cc.clientId}:gridForm:gridPanel" actionListener="#{cc.attrs.prepareCopy}"/>  
                    <p:menuitem value="#{cc.attrs.deletePopupMenuText}" update=":#{cc.clientId}:gridForm:formPanel :#{cc.clientId}:gridForm:gridPanel" actionListener="#{cc.attrs.deleteSelected}"/>  
                </p:contextMenu> 
            </h:panelGroup>

            <br/>

            <p:panelGrid id="formPanel" columns="2">
                <f:facet name="header" rendered="#{not empty cc.attrs.formHeaderText}">
                    #{cc.attrs.formHeaderText}
                </f:facet>
                <cc:renderFacet name="formFields"/>
                <f:facet name="footer">
                    <p:commandButton action="#{cc.attrs.save}" value="#{cc.attrs.saveButtonText}" update=":#{cc.clientId}:gridForm:formPanel :#{cc.clientId}:gridForm:gridPanel"/>
                </f:facet>
            </p:panelGrid>
        </h:form>
    </cc:implementation>
</html>